package com.aboo.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

/**
 * @author Yata Aboo
 * @Date 2012-9-17
 * @Desc request工具包
 */
public class RequestUtil {

	/**
	 * 从request中获取parameter参数，取值为String类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @param defaultValue 如果为空时，取的默认值
	 * @return 参数值
	 */
	public static String getParameterStringDefault(HttpServletRequest request, String key, String defaultValue) {
		if (StringUtils.isEmpty(request.getParameter(key))) {
			return defaultValue;
		}
		return request.getParameter(key);
	}
	
	/**
	 * 从request中获取parameter参数，取值为String类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @return 参数值
	 */
	public static String getParameterString(HttpServletRequest request, String key) {
		return request.getParameter(key);
	}
	
	/**
	 * 从request中获取attribute参数，取值为String类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @param defaultValue 如果为空时，取的默认值
	 * @return 参数值
	 */
	public static String getAttributeStringDefault(HttpServletRequest request, String key, String defaultValue) {
		if (StringUtils.isEmpty((String) request.getAttribute(key))) {
			return defaultValue;
		}
		return (String) request.getAttribute(key);
	}
	
	/**
	 * 从request中获取attribute参数，取值为String类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @return 参数值
	 */
	public static String getAttributeString(HttpServletRequest request, String key) {
		return (String) request.getAttribute(key);
	}
	
	/**
	 * 从request中获取parameter参数，取值为Integer类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @param defaultValue 如果为空时，取的默认值
	 * @return 参数值
	 */
	public static Integer getParameterIntegerDefault(HttpServletRequest request, String key, Integer defaultValue) {
		if (StringUtils.isEmpty(request.getParameter(key))) {
			return defaultValue;
		}
		return new Integer(request.getParameter(key));
	}
	
	/**
	 * 从request中获取parameter参数，取值为Integer类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @return 参数值
	 */
	public static Integer getParameterInteger(HttpServletRequest request, String key) {
		return getParameterIntegerDefault(request, key, null);
	}
	
	/**
	 * 从request中获取attribute参数，取值为Integer类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @param defaultValue 如果为空时，取的默认值
	 * @return 参数值
	 */
	public static Integer getAttributeIntegerDefault(HttpServletRequest request, String key, Integer defaultValue) {
		Integer val = (Integer) request.getAttribute(key);
		if (val == null) {
			return defaultValue;
		}
		return val;
	}
	
	/**
	 * 从request中获取attribute参数，取值为Integer类型
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @return 参数值
	 */
	public static Integer getAttributeInteger(HttpServletRequest request, String key) {
		return getAttributeIntegerDefault(request, key, null);
	}
	
	/**
	 * 向request的attribute中插入值
	 * @param request HttpServletRequest
	 * @param key 参数键名称
	 * @param value 参数值
	 */
	public static void setAttribute(HttpServletRequest request, String key, Object value) {
		request.setAttribute(key, value);
	}
	
	/**
	 * 返回操作者IP
	 * @param request HttpServletRequest
	 * @return 操作者IP
	 */
	public static String getOperationIP(HttpServletRequest request) {
		return request.getRemoteHost();
	}
	
}
